﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using Autodesk.Revit.UI;

namespace YArchitech.HVAC
{
	public partial class FrmListCate : System.Windows.Forms.Form
	{
		private void FrmParentMethod()
		{
			if (this.classSel != null)
			{
				this.classSel();
			}
		}

		public FrmListCate(string strCurrApp, ExternalCommandData cmdData)
		{
			this.cmdDataForm = cmdData;
			this.currentApp = strCurrApp;
			this.InitializeComponent();
			base.ShowInTaskbar = false;
			this.BindPro();
		}

		public FrmListCate()
		{
			this.InitializeComponent();
			base.ShowInTaskbar = false;
		}

		private void BindPro()
		{
			if (this.cmdDataForm.Application.ActiveUIDocument == null)
			{
				return;
			}
			UIDocument activeUIDocument = this.cmdDataForm.Application.ActiveUIDocument;
			if (activeUIDocument.Document.Title != "")
			{
				this.proTitle = activeUIDocument.Document.Title;
			}
			if (activeUIDocument.Document.PathName != "")
			{
				this.proPath = activeUIDocument.Document.PathName;
			}
		}

		private void BindData()
		{
			this.dataGridView1.RowHeadersVisible = false;
			this.dataGridView1.ColumnHeadersVisible = false;
			DataGridViewImageColumn dataGridViewImageColumn = new DataGridViewImageColumn();
			dataGridViewImageColumn.Name = "Drop";
			dataGridViewImageColumn.HeaderText = "下拉";
			dataGridViewImageColumn.Width = 20;
			this.dataGridView1.Columns.Insert(0, dataGridViewImageColumn);
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn.Name = "Name";
			dataGridViewTextBoxColumn.HeaderText = "类型名称";
			dataGridViewTextBoxColumn.Width = 94;
			this.dataGridView1.Columns.Insert(0, dataGridViewTextBoxColumn);
			DataGridViewCheckBoxColumn dataGridViewCheckBoxColumn = new DataGridViewCheckBoxColumn();
			dataGridViewCheckBoxColumn.Name = "isCheck";
			dataGridViewCheckBoxColumn.HeaderText = "选";
			dataGridViewCheckBoxColumn.Width = 20;
			this.dataGridView1.Columns.Insert(0, dataGridViewCheckBoxColumn);
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.LoadXml(XmlInfo.xmlCateStr);
			XmlNodeList xmlNodeList = xmlDocument.SelectNodes("Elements/Cate");
			this.dataGridView1.Rows.Add(xmlNodeList.Count);
			for (int i = 0; i < xmlNodeList.Count; i++)
			{
				if (xmlNodeList.Item(i).Attributes["Check"].Value == "true")
				{
					this.dataGridView1.Rows[i].Cells[0].Value = true;
				}
				else
				{
					this.dataGridView1.Rows[i].Cells[0].Value = false;
				}
				this.dataGridView1.Rows[i].Cells[1].Value = xmlNodeList.Item(i).Attributes["Name"].Value;
				if (i == 0)
				{
					this.dataGridView1.Rows[i].Cells[2].Value = this.imageList1.Images[2];
				}
				else
				{
					this.dataGridView1.Rows[i].Cells[2].Value = this.imageList1.Images[3];
				}
			}
			this.dataGridView1.Width = 134;
			this.dataGridView1.Height = xmlNodeList.Count * 23;
			this.panel1.Width = 141;
			this.panel1.Height = this.dataGridView1.Height + 2;
			base.Width = this.panel1.Width;
			base.Height = this.panel1.Height;
			this.textBox1.Focus();
		}

		private void FrmListCate_Load(object sender, EventArgs e)
		{
			this.BindData();
			this.timer1.Start();
		}

		private void panel1_Paint(object sender, PaintEventArgs e)
		{
			WinFormTools.SetPanelColor(this.panel1, e);
		}

		private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			if (e.ColumnIndex == 2 && e.RowIndex == 0)
			{
				base.Hide();
				return;
			}
			bool flag = (bool)this.dataGridView1.Rows[e.RowIndex].Cells[0].Value;
			if (e.ColumnIndex != 0)
			{
				this.dataGridView1.Rows[e.RowIndex].Cells[0].Value = !flag;
			}
			XmlInfo.ChangeSelCate(e.RowIndex, !flag);
			if (e.RowIndex == 0)
			{
				this.FrmParentMethod();
			}
		}

		private void timer1_Tick(object sender, EventArgs e)
		{
			UIDocument activeUIDocument = this.cmdDataForm.Application.ActiveUIDocument;
			if (System.Windows.Forms.Form.ActiveForm != null)
			{
				if (this.proTitle != activeUIDocument.Document.Title || this.proPath != activeUIDocument.Document.PathName)
				{
					base.Hide();
				}
				return;
			}
			if (WinFormTools.GetForegroundWindow().ToString() != this.currentApp)
			{
				base.Hide();
				return;
			}
			if (this.cmdDataForm.Application.ActiveUIDocument == null)
			{
				base.Hide();
				return;
			}
			if (activeUIDocument.Document.Title != this.proTitle)
			{
				base.Hide();
				return;
			}
			if (activeUIDocument.Document.PathName != this.proPath)
			{
				base.Hide();
			}
		}

		public FrmListCate.delegateSel classSel;

		private string proTitle = "";

		private string proPath = "";

		private string currentApp = "";

		private ExternalCommandData cmdDataForm;

		public delegate void delegateSel();
	}
}
